package com.jrapider.designer.service;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jrapider.FreemarkerConfig;
import com.jrapider.mapper.SysEntityFieldMapper;
import com.jrapider.mapper.SysEntityMapper;
import com.jrapider.mapper.SysPageMapper;
import com.jrapider.om.JSONResult;
import com.jrapider.om.SysEntity;
import com.jrapider.om.SysEntityField;
import com.jrapider.om.SysPage;
import freemarker.template.Configuration;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/11/17.
 */
@Service
@Log4j2
public class SysPageService extends ServiceImpl<SysPageMapper, SysPage> {

    @Autowired
    private SysEntityMapper sysEntityMapper;

    @Autowired
    private SysEntityFieldMapper sysEntityFieldMapper;

    @Autowired
    private FreemarkService freemarkService;


    public JSONObject genCode(SysPage sysPage) {

        Long entityId = sysPage.getEntityId();

        SysEntity sysEntity = sysEntityMapper.selectById(entityId);
        SysEntityField con = new SysEntityField();
        con.setEntityId(entityId);
        List<SysEntityField> fieldList = sysEntityFieldMapper.selectList(new QueryWrapper<>(con));
        try {
            freemarkService.genCode(sysEntity, sysPage, fieldList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return JSONResult.newBuilder().fail(e.getMessage()).build();
        }

        return JSONResult.newBuilder().success("生成成功").setData(sysPage.getId()).build();
    }


}
